<template>
  <div class="points-ranking-page">
    <div class="container">
      <div class="page-header">
        <h1 class="page-title">积分排行榜</h1>
        <div class="page-desc">积分可以通过完成题目、每日签到和参与讨论获得</div>
      </div>
      
      <el-card class="ranking-card">
        <div class="top-three">
          <div v-for="(user, index) in topThree" :key="user.id" class="top-user" :class="`rank-${index+1}`">
            <div class="medal">{{ getMedalEmoji(index) }}</div>
            <el-avatar :size="index === 0 ? 100 : 80" :src="user.avatar" class="top-avatar">
              {{ user.name.substr(0, 1) }}
            </el-avatar>
            <div class="top-name">{{ user.name }}</div>
            <div class="top-points">{{ user.point }}</div>
            <div class="top-rank">{{ index + 1 }}</div>
          </div>
        </div>
        
        <el-divider>其他排名</el-divider>
        
        <div v-loading="loading">
          <el-table :data="otherUsers" stripe style="width: 100%">
            <el-table-column label="排名" width="80">
              <template #default="scope">
                <div class="rank-number">{{ scope.$index + 4 }}</div>
              </template>
            </el-table-column>
            
            <el-table-column label="用户" min-width="200">
              <template #default="scope">
                <div class="user-cell">
                  <el-avatar :size="40" :src="scope.row.avatar">{{ scope.row.name.substr(0, 1) }}</el-avatar>
                  <span class="user-name">{{ scope.row.name }}</span>
                </div>
              </template>
            </el-table-column>
            
            <el-table-column label="积分" width="120">
              <template #default="scope">
                <div class="user-points">{{ scope.row.point }}</div>
              </template>
            </el-table-column>
          </el-table>
          
          <div class="pagination-container">
            <el-pagination
              v-model:current-page="currentPage"
              v-model:page-size="pageSize"
              :page-sizes="[10, 20, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </el-card>
      
      <el-card class="points-rules-card">
        <template #header>
          <div class="card-header">
            <span>积分规则</span>
          </div>
        </template>
        <ul class="rules-list">
          <li><el-tag type="success">签到</el-tag> 每日签到获得 5 积分</li>
          <li><el-tag type="primary">做题</el-tag> 完成一道简单题目获得 10 积分</li>
          <li><el-tag type="primary">做题</el-tag> 完成一道中等题目获得 20 积分</li>
          <li><el-tag type="primary">做题</el-tag> 完成一道困难题目获得 30 积分</li>
          <li><el-tag type="warning">讨论</el-tag> 发表讨论获得 3 积分</li>
          <li><el-tag type="warning">讨论</el-tag> 回复讨论获得 1 积分</li>
          <li><el-tag type="info">其他</el-tag> 参与课堂活动获得 5-20 积分</li>
        </ul>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import request from '../utils/request'

const loading = ref(false)
const ranking = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const topThree = computed(() => ranking.value.slice(0, 3))
const otherUsers = computed(() => ranking.value.slice(3))

onMounted(() => {
  fetchRanking()
})

async function fetchRanking() {
  loading.value = true
  try {
    // 这里应该调用API获取积分排行
    // 由于API文档中没有直接的积分排行API，这里模拟数据
    // 实际开发时应替换为真实API
    await new Promise(resolve => setTimeout(resolve, 500))
    
    // 模拟数据
    ranking.value = [
      { id: 1, name: '张三', point: 520, avatar: '' },
      { id: 2, name: '李四', point: 485, avatar: '' },
      { id: 3, name: '王五', point: 450, avatar: '' },
      { id: 4, name: '赵六', point: 390, avatar: '' },
      { id: 5, name: '钱七', point: 320, avatar: '' },
      { id: 6, name: '孙八', point: 290, avatar: '' },
      { id: 7, name: '周九', point: 240, avatar: '' },
      { id: 8, name: '吴十', point: 210, avatar: '' },
      { id: 9, name: '郑十一', point: 180, avatar: '' },
      { id: 10, name: '王十二', point: 150, avatar: '' },
      { id: 11, name: '冯十三', point: 120, avatar: '' },
      { id: 12, name: '陈十四', point: 100, avatar: '' },
    ]
    
    total.value = 50 // 模拟总记录数
  } catch (error: any) {
    console.error('获取积分排行失败:', error)
    ElMessage.error(error.message || '获取积分排行失败')
  } finally {
    loading.value = false
  }
}

function handleSizeChange(val: number) {
  pageSize.value = val
  fetchRanking()
}

function handleCurrentChange(val: number) {
  currentPage.value = val
  fetchRanking()
}

function getMedalEmoji(index: number) {
  const medals = ['🥇', '🥈', '🥉']
  return medals[index] || ''
}
</script>

<style scoped>
.points-ranking-page {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-title {
  font-size: 2rem;
  color: #303133;
  margin-bottom: 10px;
}

.page-desc {
  color: #606266;
  font-size: 1rem;
}

.ranking-card {
  margin-bottom: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.top-three {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 30px;
  padding: 30px 0;
  margin-bottom: 20px;
}

.top-user {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  padding: 20px;
  border-radius: 10px;
  transition: all 0.3s;
}

.top-user:hover {
  transform: translateY(-5px);
}

.rank-1 {
  order: 2;
  background: linear-gradient(135deg, #f6e681 0%, #ffd84d 100%);
  box-shadow: 0 6px 16px rgba(255, 216, 77, 0.3);
}

.rank-2 {
  order: 1;
  background: linear-gradient(135deg, #e0e0e0 0%, #c0c0c0 100%);
  box-shadow: 0 6px 16px rgba(192, 192, 192, 0.3);
}

.rank-3 {
  order: 3;
  background: linear-gradient(135deg, #e6be94 0%, #cd7f32 100%);
  box-shadow: 0 6px 16px rgba(205, 127, 50, 0.3);
}

.medal {
  font-size: 2rem;
  margin-bottom: 10px;
}

.top-avatar {
  border: 4px solid #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 15px;
}

.top-name {
  font-size: 1.2rem;
  font-weight: bold;
  margin-bottom: 5px;
}

.top-points {
  font-size: 1.5rem;
  font-weight: bold;
  color: #303133;
}

.top-rank {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 50%;
  font-weight: bold;
}

.user-cell {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-name {
  font-weight: 500;
}

.user-points {
  font-weight: bold;
  color: #409eff;
}

.rank-number {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f2f5;
  border-radius: 50%;
  font-weight: bold;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.points-rules-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  font-size: 1.2rem;
  font-weight: 500;
}

.rules-list {
  padding-left: 20px;
  list-style-type: none;
}

.rules-list li {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
}

@media (max-width: 768px) {
  .top-three {
    flex-direction: column;
    align-items: center;
    gap: 20px;
  }
  
  .top-user {
    order: unset !important;
  }
  
  .rank-1 {
    order: 1 !important;
  }
  
  .rank-2 {
    order: 2 !important;
  }
  
  .rank-3 {
    order: 3 !important;
  }
}
</style> 